﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XiaoMan.Service
{
    /// <summary>
    /// 图片处理方法，调用该类的方法最好使用多线程调用
    /// </summary>
    public class ImageHelper
    {
        /// <summary>
        /// 保存上传的头像到/CommPic/HeadImages/路径下
        /// </summary>
        /// <param name="Base64Str">Base64字符串</param>
        /// <param name="Suffix">图片后缀,不填默认jpg格式</param>
        /// <returns></returns>
        public bool SaveHeadImagesToFile(string Base64Str, string Suffix = "jpg")
        {
            try
            {
                if (Base64Str != "")
                {
                    string ImgUrl = System.Configuration.ConfigurationManager.AppSettings["ImgUrl"];//图片保存的绝对路径，填写在配置文件中，每次发布需要修改配置文件
                    Base64Str = Base64Str.Replace(' ', '+').Split(',')[1];
                    byte[] bytes = new byte[System.Convert.FromBase64String(Base64Str).Length];
                    bytes = System.Convert.FromBase64String(Base64Str);

                    string fileName = TimeHelp.GetTimeStamp(DateTime.Now) + "." + Suffix;
                    string MapPath = "";
                    MapPath = Path.GetFullPath(ImgUrl + "/CommPic/HeadImages/");
                    Base64Str = "/CommPic/HeadImages/" + fileName;

                    if (MapPath != "")
                    {
                        if (!Directory.Exists(MapPath))
                        {
                            Directory.CreateDirectory(MapPath);
                        }
                        FileStream fs = File.Create(MapPath + fileName);
                        fs.Write(bytes, 0, bytes.Length);
                        fs.Flush();
                        fs.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 保存上传的帖子图片到/CommPic/Forum_Pictures/路径下
        /// </summary>
        /// <param name="Base64Str">Base64字符串</param>
        /// <param name="Suffix">图片后缀，不填默认jpg格式</param>
        /// <returns></returns>
        public bool SaveForumPicturesToFile(string Base64Str, string Suffix = "jpg")
        {
            try
            {
                if (Base64Str != "")
                {
                    string ImgUrl = System.Configuration.ConfigurationManager.AppSettings["ImgUrl"];//图片保存的绝对路径，填写在配置文件中，每次发布需要修改配置文件
                    Base64Str = Base64Str.Replace(' ', '+').Split(',')[1];
                    byte[] bytes = new byte[System.Convert.FromBase64String(Base64Str).Length];
                    bytes = System.Convert.FromBase64String(Base64Str);

                    string fileName = TimeHelp.GetTimeStamp(DateTime.Now) + "." + Suffix;
                    string MapPath = "";
                    MapPath = Path.GetFullPath(ImgUrl + "/CommPic/Forum_Pictures/");
                    Base64Str = "/CommPic/Forum_Pictures/" + fileName;

                    if (MapPath != "")
                    {
                        if (!Directory.Exists(MapPath))
                        {
                            Directory.CreateDirectory(MapPath);
                        }
                        FileStream fs = File.Create(MapPath + fileName);
                        fs.Write(bytes, 0, bytes.Length);
                        fs.Flush();
                        fs.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
